<?php
/*
	Muffinz CMS - A website content management system.
	Copyright (C) 2010-2011 Sigitas Zelenkovas

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
	
	Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/

if($exec == 1){
	$cTheme->SetTitle("Login");
	if($_SESSION['user'] == null){
		$k = false;
		$buffer = false;
		
		if(isset($_POST['lsubmit'])){
			if(isset($_POST['lname']) && $_POST['lname'] != ""){}else { $buffer .= "<li>Username was not entered</li>"; }
			if(isset($_POST['lpass']) && $_POST['lpass'] != ""){}else { $buffer .= "<li>Password was not entered</li>"; }
			
			$data = $q->Send("SELECT * FROM users WHERE username=\"".$q->Escape($_POST['lname'])."\" AND password=\"".hash("sha512", $_POST['lpass'])."\"");
			if($q->Rows($data) == 1){}else { $buffer .= "<li>User was not found.</li>"; }
			
			if($buffer == false){
				$k = true;
				$user->ParseSettings($data);
				
				$buffer = "";
				$cTheme->AddModule("Login page", $buffer, 10)or die($cTheme->Error(__LINE__));
				if(isset($_SESSION['redi']) && $_SESSION['redi'] != ""){ $b = $_SESSION['redi']; unset($_SESSION['redi']); header("Location: $b"); exit; }else { header("Location: {$_SESSION['sys']['core_link_full']}{$_SESSION['sys']['core_link']}/"); exit; }
			}
		}
		
		if(!$k){
			$buffer  = "<form method=\"post\" action=\"\"><table border=\"0\">";
			$buffer .= "<tr><td>Username:</td><td><input type=\"text\" name=\"lname\" /></td></tr>";
			$buffer .= "<tr><td>Password:</td><td><input type=\"password\" name=\"lpass\" /></td></tr>";
			$buffer .= "<tr><td>&nbsp;</td><td><input type=\"submit\" class=\"button\" name=\"lsubmit\" value=\"Login\" /></td></tr>";
			$buffer .= "</table></form>";
			$cTheme->AddModule("Login page", $buffer, 10)or die($cTheme->Error(__LINE__));
		}
	}else {
		$cTheme->AddModule("Error", "You're already loged in.", 10)or die($cTheme->Error(__LINE__));
	}
}else { header("Location: /"); } 